/**
   Copyright 2012 Shakil Siraj (shakil.siraj@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.googlecode.objectify.test.dataloader;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;

/**
 * Helper class to create Objectify {@link Key} and {@link Ref}, mainly used in
 * Smooks.
 * 
 * @author Shakil Siraj
 * 
 */
public class ObjectifyHelper {

	/**
	 * Creates a Long based Objectify key.
	 * 
	 * @param <T>
	 *            the Key type
	 * @param type
	 *            The entity type
	 * @param id
	 *            key id in String form, passed on from Smooks
	 * @return The newly created Objectify key
	 * @throws Exception
	 *             thrown from Class object creation
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public final <T> Key<T> createLongBasedKey(final String type,
			final String id) throws Exception {
		Class clazz = Class.forName(type);
		return Key.create(clazz, Long.parseLong(id));
	}

	/**
	 * Creates a String based Objectify key.
	 * 
	 * @param <T>
	 *            the Key type
	 * @param type
	 *            The entity type
	 * @param id
	 *            Key id
	 * @return The newly created Objectify key
	 * @throws Exception
	 *             thrown from Class object creation
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public final <T> Key<T> createStringBasedKey(final String type,
			final String id) throws Exception {
		Class clazz = Class.forName(type);
		return Key.create(clazz, id);
	}

	/**
	 * Creates a {@link Ref} associated to a long based {@link Key}.
	 * 
	 * @param <T>
	 *            the Key type
	 * @param type
	 *            the entity type
	 * @param id
	 *            key id
	 * @return the newly created {@link Ref}
	 * @throws Exception
	 *             any thrown from underline API
	 */
	@SuppressWarnings("unchecked")
	public final <T> Ref<T> createRefFromLongBasedKey(final String type,
			final String id) throws Exception {
		return (Ref<T>) Ref.create(createLongBasedKey(type, id));
	}

	/**
	 * Creates a {@link Ref} associated to a String based {@link Key}.
	 * 
	 * @param <T>
	 *            the Key type
	 * 
	 * @param type
	 *            the entity type
	 * @param id
	 *            key id
	 * @return the newly created {@link Ref}
	 * @throws Exception
	 *             any thrown from underline API
	 */
	@SuppressWarnings("unchecked")
	public final <T> Ref<T> createRefFromStringBasedKey(final String type,
			final String id) throws Exception {
		return (Ref<T>) Ref.create(createStringBasedKey(type, id));
	}
}
